package simpleStructure;

import linked.LinkedList1;

public class LinkedListQueue<E> implements Queue<E> {


    private Node head, tail;
    private int size;

    public LinkedListQueue() {
        head = null;
        tail = null;
        size = 0;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public void enqueue(E e) {
        Node node = new Node(e);
        if (size == 0) {
            head = node;
            tail = node;
        } else {
            tail.next = node;
            tail = tail.next;
        }
        size++;
    }

    @Override
    public E dequeue() {
        if (size == 0) {
            throw new IllegalArgumentException("dequeue failed,Illegal index.");
        }
        Node retNode = head;
        head = head.next;
        retNode.next = null;
        size--;
        if (size == 0) {
            tail = null;
        }
        return retNode.data;
    }

    @Override
    public E getFront() {
        if (head == null) {
            throw new IllegalArgumentException("dequeue failed,Illegal index.");
        }
        return head.data;
    }

    private class Node {
        public E data;
        public Node next;

        public Node(E e, Node node) {
            data = e;
            next = node;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return data.toString();
        }
    }
}
